package com.mycc.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mycc.damin.TbGuides;
import com.mycc.damin.TbTicketOrders;
import com.mycc.damin.TicketOrderDto;
import com.mycc.dao.ticketOrdersDao;
import com.mycc.service.TicketOrdersService;

import com.mycc.service.TouristsService;
import com.mycc.service.ToursService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class TicketOrdersServiceImpl implements TicketOrdersService {
    private static final Logger logger = LoggerFactory.getLogger(TicketOrdersServiceImpl.class);

    @Autowired
    private ticketOrdersDao ticketOrdersDao;


    // 新增订单
    @Override
    public boolean addTicketOrders(TbTicketOrders ticketOrders) {
        // 在方法开始处添加日志记录
        logger.info("Adding ticket order: {}", ticketOrders);
        if (ticketOrders != null) {
            logger.debug("Order details - Id: {}, TouristId: {}, TourId: {}, OrderStatus: {}, OrderDate: {}",
                    ticketOrders.getId(), ticketOrders.getTouristId(), ticketOrders.getTourId(),
                    ticketOrders.getOrderStatus(), ticketOrders.getOrderDate());
        } else {
            logger.warn("Attempt to add a null ticket order.");
        }

        int x = ticketOrdersDao.insert(ticketOrders);
        if (x == 1) {
            return true;
        } else {
            return false;
        }
    }

    //取消订单
    @Override
    public boolean cancelTicketOrders(int id, String newStatus) {
        TbTicketOrders tbTicketOrders = ticketOrdersDao.selectById(id);
        if (tbTicketOrders != null) {
            // 直接设置订单状态
            tbTicketOrders.setOrderStatus(newStatus);
            // 假设ticketOrdersDao有一个update方法可以直接根据实体更新数据库
            int updatedRows = ticketOrdersDao.updateById(tbTicketOrders); // 注意：这里我假设了方法名是update，你可能需要根据你的DAO层实际情况来调整
            return updatedRows > 0;
        }
        return false;
    }

    //查询订单
    @Override
    public IPage<TicketOrderDto> getAllTicketOrders(Integer currentPage, Integer pageSize) {
        // 创建Page对象，传入当前页码和每页大小
        Page<TicketOrderDto> page = new Page<>(currentPage, pageSize);
        // 调用DAO层的分页查询方法，传入Page对象
        IPage<TicketOrderDto> resultPage = ticketOrdersDao.selectTicketOrdersWithRelations(page);
        return resultPage;
    }


    //通过id查询订单
    @Override
    public TicketOrderDto getTicketOrdersById(int id) {
        // 我直接调用DAO层的方法，
        return ticketOrdersDao.selectTicketOrderById(id);
    }

    //通过id修改订单
    @Override
    public boolean updateTicketOrderById(Long id, TicketOrderDto ticketOrderDto) {
        //        int rowsAffected = ticketOrdersDao.updateTicketOrder(id, ticketOrderDto);
        //        return rowsAffected > 0;
        return false;
    }
}
